﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class YHGL_YHAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("12"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (!string.IsNullOrEmpty(CommonUse.GetID()))
        InitData();
      BindXTQX();
      BindDW();
    }
  }

  protected void BindDW()
  {
    DataSet ds = DWGL.GetDS_YH(CommonUse.GetID());
    ListItem li = null;
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      li = new ListItem();
      li.Text = ds.Tables[0].Rows[i]["dwmc"].ToString();
      li.Value = ds.Tables[0].Rows[i]["dwid"].ToString();
      li.Selected = Convert.ToBoolean(ds.Tables[0].Rows[i]["qx"].ToString());
      cblDW.Items.Add(li);
    }
  }

  protected void BindXTQX()
  {
    DataSet ds = ProjectPrivate.GetXTGN(CommonUse.GetID());
    ListItem li = null;
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      li = new ListItem();
      li.Text = ds.Tables[0].Rows[i]["gnmc"].ToString();
      li.Value = ds.Tables[0].Rows[i]["xtgnid"].ToString();
      li.Selected = Convert.ToBoolean(ds.Tables[0].Rows[i]["qx"].ToString());
      CheckBoxList1.Items.Add(li);
    }
  }

  protected void InitData()
  {
    SqlDataReader dr =YHGL.GetXX(CommonUse.GetID());
    if (dr.HasRows)
    {
      dr.Read();
      txtName.Text = dr["dlm"].ToString();
      txtPWD.Attributes.Add("value", DESEncrypt.Decrypt(dr["mm"].ToString()));
      txtPWD3.Attributes.Add("value", DESEncrypt.Decrypt(dr["mm"].ToString()));
      //txtPWD.Text = DESEncrypt.Decrypt(dr["mm"].ToString());
      //txtZW.Text = dr["zw"].ToString();
      txtUserName.Text = dr["xm"].ToString();
      txtName.Enabled = false;
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(CommonUse.GetID()))
      Insert();
    else
      Update();
  }

  //插入
  protected void Insert()
  {
    if (CommonUse.ExistID("sys_user", "dlm", txtName.Text))
    {
      lbMessage.Text = "此用户名已存在";
      return;
    }
    Save();
  }

  protected void Save()
  {
    List<CommandInfo> cis = new List<CommandInfo>();

    cis.Add(CommonUse.DeleteCI("sys_user", "dlm", CommonUse.GetID()));
    cis.Add(CommonUse.DeleteCI("sys_yhgn", "dlm", CommonUse.GetID()));
    cis.Add(CommonUse.DeleteCI("sys_yhdw", "dlm", CommonUse.GetID()));

    ArrayList arrValue = new ArrayList();
    arrValue.Add(txtName.Text);
    arrValue.Add(DESEncrypt.Encrypt(txtPWD.Text));
    arrValue.Add(txtUserName.Text);
    ArrayList arrLengh = new ArrayList();
    arrLengh.Add("20");
    arrLengh.Add("50");
    arrLengh.Add("20");
    cis.Add(CommonUse.InsertCI("sys_user", arrValue, arrLengh));

    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
      if (CheckBoxList1.Items[i].Selected)
      {
        arrValue = new ArrayList();
        arrValue.Add(txtName.Text);
        arrValue.Add(CheckBoxList1.Items[i].Value);
        arrLengh = new ArrayList();
        arrLengh.Add("20");
        arrLengh.Add("20");
        cis.Add(CommonUse.InsertCI("sys_yhgn", arrValue, arrLengh));
      }
    }

    for (int i = 0; i < cblDW.Items.Count; i++)
    {
      if (cblDW.Items[i].Selected)
      {
        arrValue = new ArrayList();
        arrValue.Add(txtName.Text);
        arrValue.Add(cblDW.Items[i].Value);
        arrLengh = new ArrayList();
        arrLengh.Add("20");
        arrLengh.Add("20");
        cis.Add(CommonUse.InsertCI("sys_yhdw", arrValue, arrLengh));
      }
    }

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = txtName.Text + "保存成功";
    }
  }

  //修改
  protected void Update()
  {
    Save();
  }

  protected void btnQuery_Click(object sender, EventArgs e)
  {
    //CommonUse.BindCLDW(ddlDW, "0", txtDW.Text);
  }
}
